package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AlertDataDto;
import com.zzyl.dto.AlertDataHandleDto;
import com.zzyl.entity.User;
import com.zzyl.mapper.AlertDataMapper;
import com.zzyl.mapper.UserMapper;
import com.zzyl.service.AlertDataService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.AlertDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AlertDataServiceImpl implements AlertDataService {

    @Autowired
    private AlertDataMapper deviceMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResponse<AlertDataVo> pageQuery(AlertDataDto dto) {
        if (dto.getEndTime()!=null&&dto.getEndTime()>0){
           dto.setEndData(LocalDateTimeUtil.of(dto.getEndTime()));
        }

        if (dto.getStartTime()!=null&&dto.getStartTime()>0){
            dto.setStartData(LocalDateTimeUtil.of(dto.getStartTime()));
        }

       //开启分页  ;
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        Page<AlertDataVo> pages = deviceMapper.pageQuery(dto);
        return PageResponse.of(pages,AlertDataVo.class);


    }

    @Override
    public void handleAlertData(Long id, AlertDataHandleDto dto) {
        Long mgtUserId = UserThreadLocal.getMgtUserId();
        User user = userMapper.selectByPrimaryKey(mgtUserId);

        deviceMapper.handleAlertData(id,dto.getProcessingResult(),dto.getProcessingTime(),mgtUserId,user.getNickName());
    }
}
